package cn.shiwei.user.controller;

import cn.shiwei.basic.exception.MyException;
import cn.shiwei.basic.util.RequestStatus;
import cn.shiwei.user.domain.LoginDto;
import cn.shiwei.user.service.ILoginService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
@Api(tags = "登录接口",description = "登录接口详细描述")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    @PostMapping("/account")
    private RequestStatus login(@RequestBody LoginDto loginDto){
        try {
            return loginService.login(loginDto);
        } catch (MyException e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("系统繁忙");
        }
    }

    @PostMapping("/wechat")
    private RequestStatus weChatLogin(@RequestBody Map<String, String> map){
        try {
            return loginService.weChatLogin(map);
        } catch (MyException e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage(e.getMessage());
        }  catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("系统繁忙");
        }
    }

    @PostMapping("/binder/wechat")
    private RequestStatus binder(@RequestBody Map<String, String> param){
        try {
            Map<String, Object> map = loginService.binder(param);
            return RequestStatus.me().setResultObj(map);
        } catch (MyException e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage(e.getMessage());
        }  catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("系统繁忙");
        }
    }

}
